<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>任务调度器测试页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        .section {
            margin-bottom: 30px;
        }
        .section h2 {
            color: #333;
            border-bottom: 2px solid #007bff;
            padding-bottom: 10px;
        }
        .btn {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            margin: 5px;
        }
        .btn:hover {
            background-color: #0056b3;
        }
        .btn-danger {
            background-color: #dc3545;
        }
        .btn-danger:hover {
            background-color: #c82333;
        }
        .btn-success {
            background-color: #28a745;
        }
        .btn-success:hover {
            background-color: #218838;
        }
        .status {
            padding: 10px;
            border-radius: 4px;
            margin: 10px 0;
        }
        .status.success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .status.error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .status.info {
            background-color: #d1ecf1;
            color: #0c5460;
            border: 1px solid #bee5eb;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        .form-group input, .form-group textarea {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .result {
            background-color: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 4px;
            padding: 15px;
            margin-top: 10px;
            white-space: pre-wrap;
        }
    </style>
</head>
<body>
    <h1>任务调度器测试页面</h1>
    
    <div class="container">
        <div class="section">
            <h2>调度器状态</h2>
            <button class="btn" onclick="getSchedulerStatus()">获取调度器状态</button>
            <div id="schedulerStatus" class="result"></div>
        </div>
    </div>

    <div class="container">
        <div class="section">
            <h2>任务执行测试</h2>
            <div class="form-group">
                <label for="jobId">任务ID:</label>
                <input type="text" id="jobId" value="test_job_001" placeholder="输入任务ID">
            </div>
            <div class="form-group">
                <label for="jobName">任务名称:</label>
                <input type="text" id="jobName" value="testJob" placeholder="输入任务名称">
            </div>
            <div class="form-group">
                <label for="ip">客户端IP:</label>
                <input type="text" id="ip" value="127.0.0.1:8080" placeholder="输入客户端IP地址">
            </div>
            <button class="btn btn-success" onclick="executeTask()">发送任务执行请求</button>
            <button class="btn btn-danger" onclick="cancelTask()">发送任务取消请求</button>
            <div id="executeResult" class="result"></div>
        </div>
    </div>

    <div class="container">
        <div class="section">
            <h2>任务状态查询</h2>
            <div class="form-group">
                <label for="queryJobId">任务ID:</label>
                <input type="text" id="queryJobId" value="test_job_001" placeholder="输入要查询的任务ID">
            </div>
            <button class="btn" onclick="getTaskStatus()">查询任务状态</button>
            <button class="btn" onclick="getAllTaskStatus()">查询所有任务状态</button>
            <div id="statusResult" class="result"></div>
        </div>
    </div>

    <div class="container">
        <div class="section">
            <h2>快速测试</h2>
            <button class="btn btn-success" onclick="testExecute()">测试任务执行流程</button>
            <button class="btn btn-danger" onclick="testCancel()">测试任务取消流程</button>
            <div id="testResult" class="result"></div>
        </div>
    </div>

    <script>
        const API_BASE = '/api/scheduler';

        function showResult(elementId, data, isError = false) {
            const element = document.getElementById(elementId);
            element.className = 'result ' + (isError ? 'error' : 'success');
            element.textContent = JSON.stringify(data, null, 2);
        }

        function showError(elementId, message) {
            const element = document.getElementById(elementId);
            element.className = 'result error';
            element.textContent = '错误: ' + message;
        }

        async function makeRequest(url, method = 'GET', data = null) {
            try {
                const options = {
                    method: method,
                    headers: {
                        'Content-Type': 'application/json',
                    }
                };
                
                if (data) {
                    options.body = JSON.stringify(data);
                }

                const response = await fetch(url, options);
                const result = await response.json();
                
                if (!response.ok) {
                    throw new Error(result.message || '请求失败');
                }
                
                return result;
            } catch (error) {
                throw error;
            }
        }

        async function getSchedulerStatus() {
            try {
                const result = await makeRequest(API_BASE + '/status');
                showResult('schedulerStatus', result);
            } catch (error) {
                showError('schedulerStatus', error.message);
            }
        }

        async function executeTask() {
            try {
                const jobId = document.getElementById('jobId').value;
                const jobName = document.getElementById('jobName').value;
                const ip = document.getElementById('ip').value;
                
                const data = {
                    jobId: jobId,
                    jobName: jobName,
                    ip: ip,
                    success: true,
                    result: '任务执行请求'
                };
                
                const result = await makeRequest(API_BASE + '/execute-task', 'POST', data);
                showResult('executeResult', result);
            } catch (error) {
                showError('executeResult', error.message);
            }
        }

        async function cancelTask() {
            try {
                const jobId = document.getElementById('jobId').value;
                const jobName = document.getElementById('jobName').value;
                const ip = document.getElementById('ip').value;
                
                const data = {
                    jobId: jobId,
                    jobName: jobName,
                    ip: ip,
                    success: true,
                    result: '任务取消请求'
                };
                
                const result = await makeRequest(API_BASE + '/cancel-task', 'POST', data);
                showResult('executeResult', result);
            } catch (error) {
                showError('executeResult', error.message);
            }
        }

        async function getTaskStatus() {
            try {
                const jobId = document.getElementById('queryJobId').value;
                const result = await makeRequest(API_BASE + '/task-status/' + jobId);
                showResult('statusResult', result);
            } catch (error) {
                showError('statusResult', error.message);
            }
        }

        async function getAllTaskStatus() {
            try {
                const result = await makeRequest(API_BASE + '/all-task-status');
                showResult('statusResult', result);
            } catch (error) {
                showError('statusResult', error.message);
            }
        }

        async function testExecute() {
            try {
                const result = await makeRequest(API_BASE + '/test-execute', 'POST');
                showResult('testResult', result);
            } catch (error) {
                showError('testResult', error.message);
            }
        }

        async function testCancel() {
            try {
                const result = await makeRequest(API_BASE + '/test-cancel', 'POST');
                showResult('testResult', result);
            } catch (error) {
                showError('testResult', error.message);
            }
        }

        // 页面加载时获取调度器状态
        window.onload = function() {
            getSchedulerStatus();
        };
    </script>
</body>
</html> 